﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace rambla.raws.rats
{
    public class formatProfiles
    {
        private ratsConnection conn;

        internal formatProfiles(ratsConnection conn)
        {
            this.conn = conn;
        }

        public Format[] items
        {
            get
            {
                return conn.getObjects<Format>("/format");
            }
        }

        public void deleteItem(Format item)
        {
            conn.deleteObject("/format/" + item.id + "/");
        }

        public void createItem(Format item)
        {
            conn.postObject<Format>("/format", item);
        }

        public void updateItem(Format item)
        {
            conn.postObject<Format>("/format/" + item.id + "/", item);
        }

        public Format getItem(string id)
        {
            return conn.getObject<Format>("/format/" + id + "/");
        }

        public Format getItemByName(string name)
        {
            Format ret = null;
            foreach (Format item in this.items)
            {
                if (item.name == name)
                {
                    ret = item;
                }
            }
            return ret;
        }
    }

    [XmlRoot(Namespace = "http://rambla.be/raws/ns-metadata/1.0", ElementName = "params")]
    public class Format
    {
        ///<summary>
        /// unique (numerical) identifier for the format profile, given by the RATS server when creating it
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string id;

        ///<summary>
        ///a unique name for the format profile, given by the client in the request
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string name;

        ///<summary>
        ///a description of the format profile
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string description;

        ///<summary>
        ///can be 'formats' (= default) or 'devices'
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string category;

        ///<summary>
        ///video, audio or snapshot container format, can be avi, mp4, 3gp, psp, mpeg, mpegts, dvd, flv, svcd, vcd, vob, asf, mov, rm, mjpeg, mpeg2video, 3g2, mp3, ogg
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string container;

        ///<summary>
        ///video codec; can be mpeg4, xvid, flv, h263, mjpeg, mpeg1video, mpeg2video, qtrle, svq3, wmv1, wmv2, huffyuv, rv20, h264
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_codec;

        ///<summary>
        ///video bitrate in kbit/s, value must be between 50 and 10000
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_bitrate;

        ///<summary>
        ///frames per second; can be 7.5, 10.0, 14.985, 15.0, 23.98, 25.0, 29.97, 30.0
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_fps;

        ///<summary>
        ///Width in pixels of the transcoded media, must be an even number between 50 and 1920. If set to 0, the width from the source file will be retained.
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_width;

        ///<summary>
        ///Height in pixels of the transcoded media, must be an even number between 50 and 1280. If set to 0, the height will be calculated automatically based on the aspect ratio. If no aspect ratio is set, the height from the source file will be retained.
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_height;

        ///<summary>
        /// Mathematical formula that defines the relation between width and height. It must be set as a decimal value (eg. 1.3334 for 4:3) and can be used to automatically calculate height from width. If set to 0, the aspect ratio will be calculated automatically from the height and width (present in the format profile or from the source file).
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_aspect_ratio;

        ///<summary>
        ///'no' (no deinterlacing), 'md' (median deinterlacer), 'li' (linear interpolate) or 'ci' (cubic interpolate)
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_deinterlace;

        ///<summary>
        ///1 = faster, 2 = better quality
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_passes;

        ///<summary>
        ///number of pixels to cut from the original video before encoding
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_crop_bottom;

        ///<summary>
        ///number of pixels to cut from the original video before encoding
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_crop_top;

        ///<summary>
        ///number of pixels to cut from the original video before encoding
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_crop_left;

        ///<summary>
        ///number of pixels to cut from the original video before encoding
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string video_crop_right;

        ///<summary>
        /// Audio channel, can be 0 to disable audio, 1 for mono and 2 for stereo
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string audio_channel;

        ///<summary>
        ///Audio codec; can be mp3, mp2, aac, pcm, amr_nb, ac3, vorbis, flac, pcm_u8.
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string audio_codec;

        ///<summary>
        /// Audio bitrate in kbit/s, up to 512kbits
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string audio_bitrate;

        ///<summary>
        ///Sample rate; can be 8000, 11025, 16000, 22000, 22050, 24000, 32000, 44000, 44100, 48000
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string audio_sample_rate;

        ///<summary>
        ///0.1:0.1,0.2:0.2 track mappings as seen by ffmpeg, separated by comma. If the source file has multiple audio tracks (eg broadcast 1 with voice-over, 1 without) it can be mapped to output audio tracks if supported by output format (eg mp4). If empty only 1 track is used for encoding to output file.
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string audio_track_mapping;

        ///<summary>
        ///ID of an existing overlay file
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string overlay;

        ///<summary>
        ///ID of an existing input profile
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string input;

        ///<summary>
        ///ID of an existing output profile 
        ///</summary>
        [XmlElement(IsNullable = true)]
        public string output;
    }
}
